<?php
namespace Api\Controller;

/**
 * 公众号
 * Class GzhController
 * @package Api\Controller
 */
class GzhController extends BaseRestController {

    public function gzhpage() {
        $curPage = I('curPage', 1, 'int');
        $pageSize = I('pageSize', 20, 'int');
        $curPage = $curPage <= 1 ? 1 : $curPage;

        $gzh = M("Gzh");

        $gzh = $gzh
            ->field('id,title,img,total,introduction')
            ->limit($pageSize)
            ->page($curPage)
            ->where("wxh != ''")
            ->order('total desc');

        $list = $gzh->select();
        $count = $gzh->where("wxh != ''")->count();

        $data['status']  = 200;
        $data['success'] = true;
        $data['curPage'] = $curPage;
        $data['totalPage'] = intval($count / $pageSize);
        $data['data'] = $list;
        $this->response($data, 'json');
    }

    /**
     *
     */
    public function page() {
        $curPage = I('curPage', 1, 'int');
        $pageSize = I('pageSize', 20, 'int');
        $curPage = $curPage <= 1 ? 1 : $curPage;
        $gzhId = I('gzhId', 0, 'int');
        $gzhTypeId = I('gzhTypeId', 0, 'int');
        $isHot = I('isHot', 0, 'int');

        $gzhContent = M("Gzhcontent");

        $gzhContent = $gzhContent
            ->field('id,gzhId,gzhname,title,imglink,dateint,view,zan,ishot,isrecommend,isvedio')
            //->limit($pageSize * $curPage . ',' . ($curPage + 1) * $pageSize)
            ->limit($pageSize)
            ->page($curPage)
            ->order('dateint desc, id desc');



        $where = '';
        $whereArr = array();
        if (!empty($isHot) && $isHot == 1) {
            $where = "(isHot = %d or isRecommend = %d)";
            $whereArr = array(1, 1);
        } else {
            if (!empty($gzhTypeId) && $gzhTypeId > 0) {
                $where = "gzhTypeId = %d";
                $whereArr = array($gzhTypeId);
            }
            if (!empty($gzhId) && $gzhId > 0) {
                $where = "gzhId = %d";
                $whereArr = array($gzhId);
            }
        }

        if (!empty($where)) {
            $list = $gzhContent->where($where, $whereArr)->select();
            $count = $gzhContent->where($where, $whereArr)->count();
        } else {
            $list = $gzhContent->select();
            $count = $gzhContent->count();
        }


        foreach ($list as $k => $v) {
            $list[$k]['date'] = date('Y-m-d H:i:s', $v['dateint']);
            unset($list[$k]['dateint']);

            $list[$k]['id'] = intval($v['id']);
            $list[$k]['gzhid'] = intval($v['gzhid']);
            $list[$k]['zan'] = intval($v['zan']);
            $list[$k]['view'] = intval($v['view']);
            $list[$k]['ishot'] = intval($v['ishot']) == 0 ? false : true;
            $list[$k]['isrecommend'] = intval($v['isrecommend']) == 0 ? false : true;
            $list[$k]['isvedio'] = intval($v['isvedio']) == 0 ? false : true;
        }

        $data['status']  = 200;
        $data['success'] = true;
        $data['curPage'] = $curPage;
        $data['totalPage'] = intval($count / $pageSize);
        $data['data'] = $list;
        $this->response($data, 'json');
    }

    public function gzhTypeList() {
        $gzhType = M("Gzhtype");

        $list = $gzhType->order('sort desc')->select();

        $data['status']  = 200;
        $data['success'] = true;

        $data['data'] = $list;
        $this->response($data, 'json');
    }

    public function banner() {
       $gzhContent = M("Gzhcontent");
       $list = $gzhContent
           ->field('id,title,imglink')
           ->where('isBanner = 1')
           ->limit(5)
           ->order('id desc')
           ->select();

       $bannerImg = array();
       $bannerTitle = array();
       $bannerId = array();

       foreach ($list as $k => $v) {
           $bannerImg[$k] = $v['imglink'];
           $bannerTitle[$k] = $v['title'];
           $bannerId[$k] = $v['id'];
       }

        // $bannerImg = array(
        //     'http://7xk9dj.com1.z0.glb.clouddn.com/banner/imgs/7.png',
        //     'http://7xk9dj.com1.z0.glb.clouddn.com/banner/imgs/8.png',
        //     'http://7xk9dj.com1.z0.glb.clouddn.com/banner/imgs/9.png',
        //     'http://7xk9dj.com1.z0.glb.clouddn.com/banner/imgs/10.png',
        //     'http://7xk9dj.com1.z0.glb.clouddn.com/banner/imgs/11.png',);
        // $bannerTitle = array('1', '2', '3', '4', '5');

        $data['status']  = 200;
        $data['success'] = true;
        $data['data']['bannerImg'] = $bannerImg;
        $data['data']['bannerId'] = $bannerId;
        $data['data']['bannerTitle'] = $bannerTitle;
        $this->response($data, 'json');
    }


    public function getOne() {
        $id = I('id', 0, 'int');

        if (!empty($id) && $id > 0) {
            $gzhContent = M("Gzhcontent");
            $gzhText = M("Gzhtext");
            $first = $gzhContent->where("id = %d", array($id))->select();


            if (!empty($first)) {
                $text = $gzhText->field('content')->where("gzhcontentId = %d", array($id))->select();

                $arr = $first[0];
                if (!empty($text)) {
                    $arr['content'] = $text[0]['content'];
                } else {
                    $arr['content'] = '';
                }


                $gzhContent->where("id = %d", array($id))->setInc('view', 1, 60);

                $data['status'] = 200;
                $data['success'] = true;
                $data['data'] = $arr;
                $this->response($data, 'json');
            } else {
                $data['status']  = 400;
                $data['success'] = false;
                $data['data'] = array();
                $this->response($data, 'json');
            }
        } else {
            $data['status']  = 400;
            $data['success'] = false;
            $data['data'] = array();
            $this->response($data, 'json');
        }
    }


    public function addOne() {
        $gzhContent = M("Gzhcontent");

        $gzhContent->create();
    }

}
